function bases = chooseBases(weights, num_samples)

weights_sum = [0 cumsum(weights(:)'/sum(weights))];
num_pieces = length(weights_sum-1);

samples = rand(1, num_samples);
[samples samples_index] = sort(samples);

[ignored, index] = sort([weights_sum samples]);;
index = max([find(index>num_pieces)-(1:num_samples); ones(1, num_samples)]);

index(samples_index) = index;

bases = index;
